<?php

/**
 * Application bootstrap
 *
 * @uses    Zend_Application_Bootstrap_Bootstrap
 *
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	public function _initSession(){
		$ug=( isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
		$ip=Zen_User::getip();
		Zend_Session::start(array(
			'name'=>md5($ug.$ip),
			'cookie_httponly'=>true
		));
	}
	
	public function _initCache() {
		$cache_dir = $this->getOption ('cache_dir');
		if (! file_exists ( $cache_dir)) {
			mkdir ( $cache_dir, 0777, true );
		}
		$classFileIncCache= $cache_dir.'pluginLoaderCache.php';
		if (file_exists ( $classFileIncCache )){
			include_once $classFileIncCache;
		}
		Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
		Zen_Cache_Factory::initTemplate();
		
		$cache = Zen_Cache_Factory::get( 'locale' );
		Zend_Locale_Data::setCache($cache);
	}

	protected function _initAutoload() {
		Zend_Controller_Action_HelperBroker::addPrefix('Zen_Controller_Action_Helper');
		return new Zend_Application_Module_Autoloader ( 
						array (
							'namespace' => 'Zen', 
							'basePath' => dirname ( __FILE__ ) 
					 ));
	}

	protected function _initDoctype() {
		$this->bootstrap ( 'view' );
		$view = $this->getResource ( 'view' );
		$view->doctype ( 'XHTML1_STRICT' );
		$view->AddHelperPath ( 'Zen/View/Helper', 'Zen_View_Helper' );
		mb_internal_encoding('UTF8');
	}

	public function _initDatabase() {
		$this->bootstrap ( 'db' );
		$cache = Zen_Cache_Factory::get( 'db' );
		Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
	}
	
	protected function _initFront() {
		$config = new Zend_Config($this->getOptions());
		Zen_Theme::setName($config->theme);
		$theme = Zen_Theme::getName();
		$layout = $this->bootstrap('layout')->getResource('layout');		
		$layout_path = $layout->getLayoutPath();
		$layout_path[] = ROOT_PATH . "/site/themes/{$theme}/scripts/";
		$layout->setLayoutPath($layout_path)->setLayout('index');
		$front = Zend_Controller_Front::getInstance ();
		$front->registerPlugin ( new Zen_Controller_Plugin_FlashMessenger());
		$front->registerPlugin ( new Zen_Controller_Plugin_ErrorHandler());
		if ($config->debug->on) {
			$front->throwExceptions(true);
		}
	}

}